﻿using System;
using System.Collections.Generic;

using System.Text;
using HZ.DrugTrade.Model;
using System.Data;

namespace HZ.DrugTrade.BLL
{
    public class BLL_Cache
    {
        #region Fields

        private static IDictionary<string, Company> _CompanyCache = null;
        private static IDictionary<string, Hospital> _HospitalCache = null;
        private static IDictionary<string, IDictionary<string, IList<AreaDistribution>>> _AreaDistributionCache = null;
        /// <summary>
        /// 地区配送关系缓存
        /// 第一个string是ProcureCatalogID 采购目录编号
        /// 第二个string是AreaID 地区ID
        /// </summary>
        public static IDictionary<string, IDictionary<string, IList<AreaDistribution>>> AreaDistributionCache
        {
            get
            {
                if (_AreaDistributionCache == null)
                {
                    _AreaDistributionCache = new Dictionary<string, IDictionary<string, IList<AreaDistribution>>>();
                    DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable("SELECT * FROM TB_AreaDistribution");

                    foreach (DataRow item in dt.Rows)
                    {
                        string ProcureCatalogID = item["ProcureCatalogID"].ToString();
                        IDictionary<string, IList<AreaDistribution>> dictemp = null;
                        if (_AreaDistributionCache.Keys.Contains(ProcureCatalogID)) // 是否存在该产品
                        {
                            dictemp = _AreaDistributionCache[ProcureCatalogID];

                            string AreaID = item["AreaID"].ToString();
                            if (dictemp.Keys.Contains(AreaID)) // 存在该地区
                            {
                                IList<AreaDistribution> comps = _AreaDistributionCache[ProcureCatalogID][AreaID];
                                comps.Add(HZ.Data.DAL_Helper.CommonFill<AreaDistribution>(item));
                                _AreaDistributionCache[ProcureCatalogID][AreaID] = comps;
                            }
                            else // 不存在该地区
                            {
                                IList<AreaDistribution> comps = new List<AreaDistribution>();
                                comps.Add(HZ.Data.DAL_Helper.CommonFill<AreaDistribution>(item));
                                _AreaDistributionCache[ProcureCatalogID].Add(AreaID, comps);
                            }
                        }
                        else
                        {
                            dictemp = new Dictionary<string, IList<AreaDistribution>>();
                            IList<AreaDistribution> comps = new List<AreaDistribution>();
                            comps.Add(HZ.Data.DAL_Helper.CommonFill<AreaDistribution>(item));
                            dictemp.Add(item["AreaID"].ToString(), comps);
                            _AreaDistributionCache.Add(ProcureCatalogID, dictemp);
                        }

                    }


                }
                return BLL_Cache._AreaDistributionCache;

            }
            set { BLL_Cache._AreaDistributionCache = null; }
        }
        #endregion


        /// <summary>
        /// 企业缓存
        /// </summary>
        public static IDictionary<string, Company> CompanyCache
        {
            get
            {
                if (_CompanyCache == null)
                {
                    _CompanyCache = new Dictionary<string, Company>();
                    DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable("SELECT * FROM TB_Company");
                    foreach (DataRow item in dt.Rows)
                    {
                        //_CompanyCache.Add(item["CompanyID"].ToString(), HZ.Data.DAL_Helper.GetCompanyFromDataRow(item));
                        _CompanyCache.Add(item["CompanyID"].ToString(), HZ.Data.DAL_Helper.CommonFill<Company>(item));
                    }
                }
                return _CompanyCache;
            }
            set { BLL_Cache._CompanyCache = null; }
        }
        /// <summary>
        /// 医疗机构缓存
        /// </summary>
        public static IDictionary<string, Hospital> HospitalCache
        {
            get
            {
                if (_HospitalCache == null)
                {
                    _HospitalCache = new Dictionary<string, Hospital>();
                    DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable("SELECT * FROM TB_Hospital");
                    foreach (DataRow item in dt.Rows)
                    {
                        //_HospitalCache.Add(item["HospitalID"].ToString(), HZ.Data.DAL_Helper.GetHospitalFromDataRow(item));
                        _HospitalCache.Add(item["HospitalID"].ToString(), HZ.Data.DAL_Helper.CommonFill<Hospital>(item));
                    }
                }

                return BLL_Cache._HospitalCache;

            }
            set { BLL_Cache._HospitalCache = null; }
        }

        /// <summary>
        /// 是否企业地区级配送关系
        /// </summary>
        public static bool UseArea { get { return true; } }

        public static string GetCompanyNamesFromIDs(string ids)
        {
            string[] id = ids.Split(',');
            StringBuilder sb = new StringBuilder();
            bool first = true;
            foreach (var item in id)
            {
                if (first)
                {
                    sb.Append(CompanyCache[item].CompanyName);
                    first = false;
                }
                else
                {
                    sb.AppendFormat(",{0}", CompanyCache[item].CompanyName);
                }

            }

            return sb.ToString();
        }



        static IList<Model.AdjustPricePlan> _PlanList = new List<Model.AdjustPricePlan>();
        static BLL_Cache()
        {
            System.Threading.Timer threadtm = new System.Threading.Timer(new System.Threading.TimerCallback(ManagePlanList));
            threadtm.Change(0, int.Parse(System.Configuration.ConfigurationManager.AppSettings["AdjustPriceCheckTime"]) * 1000);
            System.Threading.Timer tmexecute = new System.Threading.Timer(new System.Threading.TimerCallback(ExecuteAdjustPrice));
            tmexecute.Change(0, int.Parse(System.Configuration.ConfigurationManager.AppSettings["AdjustPriceRunTime"]) * 1000);
        }

        private static void ManagePlanList(object obj)
        {
            try
            {

                string sql = "SELECT * FROM TB_AdjustPricePlan WHERE ExecuteState = 0";
                _PlanList = HZ.Data.DAL_Helper.CommonFillList<Model.AdjustPricePlan>(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql));
            }
            catch (Exception)
            {

            }
        }

        private static void ExecuteAdjustPrice(object obj)
        {
            try
            {
                List<Model.AdjustPricePlan> RemoveList = new List<AdjustPricePlan>();
                foreach (var item in _PlanList)
                {
                    if (item.ExecuteStartTime < DateTime.Now)
                    {

                        BLL.BLL_ProcurementBasic bp = new BLL_ProcurementBasic();
                        bp.ExecuteAdjustPrice(item);
                        RemoveList.Add(item);
                    }

                }
                foreach (var item in RemoveList)
                {
                    _PlanList.Remove(item);
                }
            }
            catch (Exception)
            {

            }

        }
        /// <summary>
        /// 空方法
        /// </summary>
        public static void EmptyMethod()
        { }

    }
}
